package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.dto.ReplyRuleFormDto;
import cn.livingCloud.entity.content.ReplyRule;
import cn.livingCloud.repository.ReplyRuleMapper;
import org.springframework.util.StringUtils;

/**
 * Created by DingJie  on 2015/12/9.
 */
public class ReplyRuleFormDtoUpdater {
    protected transient ReplyRuleMapper ruleMapper = BeanProvider.getBean(ReplyRuleMapper.class);

    private ReplyRuleFormDto ruleFormDto;

    public ReplyRuleFormDtoUpdater(ReplyRuleFormDto replyRuleFormDto) {
        this.ruleFormDto = replyRuleFormDto;
    }

    protected ReplyRule updateRule() {
        if (StringUtils.isEmpty(ruleFormDto.getUuid())) {
            //添加方法
            return create();
        } else {
            //更新方法
            return update();
        }
    }

    private ReplyRule update() {
        ReplyRule replyRule = ruleMapper.findReplyRule(ruleFormDto.getUuid());
        ruleFormDto.updateRule(replyRule);
        ruleMapper.update(replyRule);
        return replyRule;
    }

    private ReplyRule create() {
        ReplyRule replyRule = new ReplyRule();
        ruleFormDto.updateRule(replyRule);
        ruleMapper.save(replyRule);
        return replyRule;
    }
}
